package com.queclink.ycm.entity;

import com.alibaba.fastjson.JSONObject;
import com.queclink.ycm.common.utils.MessageUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * ResponseBean  服务器返回数据格式
 */
public class ResponseBean extends HashMap<String, Object> {
    /**
     * HTTP状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 返回的数据
     */
    private JSONObject data;

    public ResponseBean() {
        this.put("code", 200);
        this.put("msg", MessageUtils.get("system.code.success"));
        this.put("data", new JSONObject());
    }

    public static ResponseBean ok() {
        ResponseBean responseBean = new ResponseBean();
        responseBean.put("code", 200);
        responseBean.put("msg", "success");
        responseBean.put("data", new JSONObject());
        return responseBean;
    }

    /**
     * 返回成功
     *
     * @param jsonObject 业务数据 ,如果没有可以为null
     * @return
     */
    public static ResponseBean ok(JSONObject jsonObject) {
        ResponseBean responseBean = new ResponseBean();
        if (jsonObject != null) {
            responseBean.put("data", jsonObject);
        }
        return responseBean;
    }

    public static ResponseBean ok(List<?> list) {
        ResponseBean responseBean = new ResponseBean();
        JSONObject jsonObject = new JSONObject();
        if (list != null) {
            jsonObject.put("list", list);
            responseBean.put("data", jsonObject);
        }else {
            list=new ArrayList<>();
            jsonObject.put("list", list);
            responseBean.put("data", jsonObject);
        }
        return responseBean;
    }

    public static<U> ResponseBean ok(Class<U> clazz, U cla) {
        ResponseBean responseBean = new ResponseBean();
        if (cla != null) {
            U u=clazz.cast(cla);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("object", u);
            responseBean.put("data", jsonObject);
        }
        return responseBean;
    }

    public static ResponseBean ok(List<?> list, int total ) {
        ResponseBean responseBean = new ResponseBean();
        JSONObject jsonObject = new JSONObject();
        if (list != null && total>0) {
            jsonObject.put("total", total);
            jsonObject.put("rows", list);
        }else {
            list=new ArrayList<>();
            total=0;
            jsonObject.put("total", total);
            jsonObject.put("rows", list);
        }
        responseBean.put("data", jsonObject);
        return responseBean;
    }
    /**
     * 返回成功
     *
     * @param msg        消息提示
     * @param jsonObject 业务数据， 如果没有可以为null
     * @return
     */
    public static ResponseBean ok(String msg, JSONObject jsonObject) {
        ResponseBean responseBean = new ResponseBean();
        if (jsonObject != null) {
            responseBean.put("data", jsonObject);
        }
        if (msg != null) {
            responseBean.put("msg", msg);
        }
        return responseBean;
    }

    /**
     * 默认操作失败
     *
     * @return
     */
    public static ResponseBean error() {
        return error(500, MessageUtils.get("system.code.error"));
    }

    /**
     * 操作失败，自定义消息
     *
     * @param msg 操作失败自定义消息
     * @return
     */
    public static ResponseBean error(String msg) {
        return error(500, msg);
    }

    /**
     * 错误提示
     *
     * @param code 自定义错误码
     * @param msg  自定义错误消息
     * @return
     */
    public static ResponseBean error(int code, String msg) {
        ResponseBean responseBean = new ResponseBean();
        responseBean.put("code", code);
        responseBean.put("msg", msg);
        return responseBean;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(JSONObject data) {
        this.data = data;
    }

}
